package com.example.demo.smartLamp.service.impl;

import com.alibaba.druid.wall.violation.ErrorCode;
import com.example.demo.common.ApiResponse;
import com.example.demo.common.util.FrameGrabberKit;
import com.example.demo.smartLamp.VO.ImageVO;
import com.example.demo.smartLamp.VO.VideoVO;
import com.example.demo.smartLamp.entity.Image;
import com.example.demo.smartLamp.entity.Video;
import com.example.demo.smartLamp.entity.response.VideoAndImageResponse;
import com.example.demo.smartLamp.mapper.ImageMapper;
import com.example.demo.smartLamp.mapper.VideoMapper;
import com.example.demo.smartLamp.service.UploadService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class uploadServiceImpl implements UploadService {

    @Resource
    private VideoMapper videoMapper;
    @Resource
    private ImageMapper imageMapper;

    @Override
    public ApiResponse uploadVideo(MultipartFile file, String title, HttpServletRequest request) throws IOException {
        String basePath = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort()+"/home/cc/light/upload/video/";
//  /code/smartLightNew/video
        Long time = System.currentTimeMillis();

        String fileName = file.getOriginalFilename();//文件原始名称
        String suffixName = fileName.substring(fileName.lastIndexOf("."));//从最后一个.开始截取。截取fileName的后缀名
        String newFileName = time+suffixName; //文件新名称
        //设置文件存储路径，可以存放在你想要指定的路径里面
//        String rootPath="E:/code/"+ File.separator+"smartLightNew/video/"; //上传视频存放位置
        String rootPath="/home/cc/light/upload/video/"; //上传视频存放位置(服务器)
//        /home/cc/light/upload/video
//  E:/code/
//  smartLightNew/video/
        String filePath = rootPath+newFileName;
        File newFile = new File(filePath);
        //判断目标文件所在目录是否存在
        if(!newFile.getParentFile().exists()){
            //如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }

        //将内存中的数据写入磁盘
        file.transferTo(newFile);
        //视频上传保存url到数据库
        String nginxPath = "http://8.130.26.11:8080/upload/video/";
        String videoUrl = nginxPath + newFileName;

        //视频封面图处理
//        String newImgName = time+".jpg";
//        String framefile = rootPath + newImgName;
//        String imgUrlSave = basePath+newImgName;//图片最终位置路径
//        //视频截取封面图
//        String imgUrl= FrameGrabberKit.getVedioImg(videoUrl, framefile, imgUrlSave);

//        resultMap.put("videoUrl", videoUrl);
//        resultMap.put("imgUrl", imgUrl);
//        resultMap.put(ErrorCode.STATE, ErrorCode.SUCCESS);
        //System.out.println("上传的文件名为："+fileName+",后缀名为："+newFileName);

        //视频信息存入数据库
        Video video = new Video();
        video.setVideoUrl(videoUrl);
        video.setTitle(title);
        videoMapper.insert(video);
        return ApiResponse.success(200,"上传视频成功",videoUrl);
    }

    @Override
    public ApiResponse uploadImage(MultipartFile file, String title, HttpServletRequest request) throws IOException {

        String basePath = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort()+"/home/cc/light/upload/image/";

        Long time = System.currentTimeMillis();

        String fileName = file.getOriginalFilename();//文件原始名称
        String suffixName = fileName.substring(fileName.lastIndexOf("."));//从最后一个.开始截取。截取fileName的后缀名
        String newFileName = time+suffixName; //文件新名称
        //设置文件存储路径，可以存放在你想要指定的路径里面
        String rootPath="/home/cc/light/upload/image/"; //上传图片存放位置

        String filePath = rootPath+newFileName;
        File newFile = new File(filePath);
        //判断目标文件所在目录是否存在
        if(!newFile.getParentFile().exists()){
            //如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }

        //将内存中的数据写入磁盘
        file.transferTo(newFile);
        //图片上传保存url到数据库
        String nginxPath = "http://8.130.26.11:8080/upload/image/";
        String imgUrl = nginxPath + newFileName;

        //图片信息存入数据库
        Image image = new Image();
        image.setImageUrl(imgUrl);
        image.setTitle(title);
        imageMapper.insert(image);
        return ApiResponse.success(200,"上传图片成功",imgUrl);
    }

    @Override
    public ApiResponse getAllVideoAndImage() {
        List<VideoVO> videos = videoMapper.selectAllvideo();
        List<ImageVO> images = imageMapper.selectAllImage();
        VideoAndImageResponse videoAndImageResponse = new VideoAndImageResponse();
        videoAndImageResponse.setVideoList(videos);
        videoAndImageResponse.setImageList(images);
        return ApiResponse.success(200,"查找成功",videoAndImageResponse);
    }
}
